package nc.gk.gkxm.controller.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import nc.gk.gkxm.model.dto.UserLoginDto;
import nc.gk.gkxm.model.other.JwtProperties;
import nc.gk.gkxm.model.vo.UserLoginVO;
import nc.gk.gkxm.model.entity.User;
import nc.gk.gkxm.service.UserService;
import nc.gk.gkxm.utils.Result;
import nc.gk.gkxm.utils.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@Api(tags = "用户类提供小程序接口")
@RestController
@RequestMapping("/gkxm/api/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 微信登录
     * @param userLoginDto
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDto userLoginDto){
        //log.info("微信登录:{}",userLoginDTO);
        //获取登录user对象
        User user = userService.login(userLoginDto);
        //封装当前用户信息
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId",user.getUid());
        claims.put("userId",user.getCode());
        //生成JWT令牌
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        //封装VO对象
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setUid(user.getUid());
        userLoginVO.setAppid(user.getAppid());
        userLoginVO.setToken(token);
        return Result.ok(userLoginVO);
    }
}